/*
 *    Copyright 2008-2009 The Digester Tools Team
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.digestertools.annotations;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.digester.Digester;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.testng.annotations.Test;
import org.xml.sax.SAXException;

import com.digestertools.annotations.DigesterLoader;

/**
 * 
 * @author Simone Tripodi
 * @version $Id: DigesterTestCase.java 357 2009-12-16 20:00:36Z simone.tripodi $
 */
public final class DigesterTestCase {

    private final Log log = LogFactory.getLog(getClass());

    @Test(
            dataProvider = "digester-provider",
            dataProviderClass = DigesterDataProvider.class
    )
    public void assertEquals(Object expected) throws IOException, SAXException {
        Class<?> clazz = expected.getClass();

        String resource = clazz.getSimpleName() + ".xml";
        if (this.log.isDebugEnabled()) {
            this.log.info("Trying to load resource: "
                + clazz.getName().replace('.', '/')
                + ".xml");
        }
        InputStream input = clazz.getResourceAsStream(resource);

        long start = System.currentTimeMillis();
        Digester digester = DigesterLoader.createDigester(clazz);
        long time = System.currentTimeMillis() - start;
        this.log.info("----------> Digester rules of "
                + clazz
                + " generated in "
                + time
                + " ms");

        Object actual = digester.parse(input);

        if (input != null) {
            input.close();
        }

        assert expected.equals(actual) : "Expected object: <"
            + expected
            + "> but found actual object: <"
            + actual
            + ">";
    }

}
